﻿using Device.Domain;
using Device.Read.API.Applcation.Command.CarriersCommand;
using Device.Read.API.Dtos;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace Device.Read.API.Controllers
{
    /// <summary>
    /// 承运商.读.控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class CarriersModelsController : ControllerBase
    {
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public CarriersModelsController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 显示承运商
        /// </summary>
        /// <param name="command"></param>
        /// <returns>返回受影响的实体</returns>
        [HttpGet]
        public async Task<ApiResult<page>> SelectCarrierModel([FromQuery]SelectCarriersModelCommand command)
        {
            ApiResult<page> result = new ApiResult<page>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.User_Id = Convert.ToInt32(uid);
                command.Office_Id = sid;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填承运商
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<CarriersModel>> FanCarrierModel([FromQuery]FanCarriersModelCommand command)
        {
            ApiResult<CarriersModel> result = new ApiResult<CarriersModel>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 承运商导出
        /// </summary>
        /// <param name="select"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FileContentResult> GetCarrier([FromQuery]SelectCarriersModelCommand select)
        {
            var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
            var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
            select.User_Id = Convert.ToInt32(uid);
            select.Office_Id = sid;
            var obj=await _mediator.Send(select);
            IWorkbook wb = new XSSFWorkbook();
            ISheet sheet= wb.CreateSheet("承运商表");
            IRow row;
            row= sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("承运商ID");
            row.CreateCell(1).SetCellValue("承运商编号");
            row.CreateCell(2).SetCellValue("承运商名称");
            row.CreateCell(3).SetCellValue("承运商备注");
            int index = 1;
            foreach (var item in obj.list)
            {
                row = sheet.CreateRow(index);
                row.CreateCell(0).SetCellValue(item.CarriersId);
                row.CreateCell(1).SetCellValue(item.CarriersCode);
                row.CreateCell(2).SetCellValue(item.CarriersName);
                row.CreateCell(3).SetCellValue(item.Describe);
                index++;
            }
            byte[] buffe;
            using(MemoryStream ms = new MemoryStream())
            {
                wb.Write(ms);
                buffe = ms.ToArray();
            }
            string dc = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(buffe, dc, "承运商表.xlsx");
        }






















        

    }
}
